﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using lumo.display.object2D;

namespace lumo.display.animation2D
{
    /// <summary>
    /// Color animation class.
    /// </summary>
    public class ColorAnimation2D : GenericAnimation2D<IColorable>
    {
        // Private variables
        private Color? From = null;
        private Color To;
        private Counter RCounter = new Counter();
        private Counter GCounter = new Counter();
        private Counter BCounter = new Counter();
        private Counter ACounter = new Counter();

        /// <summary>
        /// Constructor
        /// </summary>
        public ColorAnimation2D(Color To, float Time, Smooth Smoothness = Smooth.None, Loop LoopMode = Loop.None, IColorable CurrentObject = null)
            : base(Time, Smoothness, LoopMode, CurrentObject)
        {
            this.To = To;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public ColorAnimation2D(Color? From, Color To, float Time, Smooth Smoothness = Smooth.None, Loop LoopMode = Loop.None, IColorable CurrentObject = null)
            : base(Time, Smoothness, LoopMode, CurrentObject)
        {
            this.From = From;
            this.To = To;
        }

        /// <summary>
        /// Check if finished
        /// </summary>
        public override bool Finished { get { return (RCounter.Finished && GCounter.Finished && BCounter.Finished && ACounter.Finished); } }

        /// <summary>
        /// Setup the values
        /// </summary>
        internal override void SetupValues()
        {
            if (!this.From.HasValue)
                this.From = this.CurrentObject.Color;
            RCounter.Reset(this.From.Value.R, this.To.R, this.Time, this.Smoothness, this.LoopMode);
            GCounter.Reset(this.From.Value.G, this.To.G, this.Time, this.Smoothness, this.LoopMode);
            BCounter.Reset(this.From.Value.B, this.To.B, this.Time, this.Smoothness, this.LoopMode);
            ACounter.Reset(this.From.Value.A, this.To.A, this.Time, this.Smoothness, this.LoopMode);
        }

        /// <summary>
        /// Update the values.
        /// </summary>
        /// <param name="Game">The game class</param>
        internal override void UpdateValues(LumoComponent Game)
        {
            RCounter.Update(Game);
            GCounter.Update(Game);
            BCounter.Update(Game);
            ACounter.Update(Game);
            this.CurrentObject.Color = new Color((byte)MathHelper.Clamp(RCounter.Value, 0, 255), (byte)MathHelper.Clamp(GCounter.Value, 0, 255), (byte)MathHelper.Clamp(BCounter.Value, 0, 255), (byte)MathHelper.Clamp(ACounter.Value, 0, 255));
        }
    }
}
